$(document).ready(function(){
    
    
    var form=$('#form');
    var tbody=$('#tbody');
    var total=$('#total');
    $(function () { //加载表格
        xhExtra.ajaxRequest('customer/getCooperationMode',{'iCUId':addressUri.getParam('id')},{},function (data) {
            initTable(data['data'],data['showDelete'],data['showEdit']);
        })
    });

    $('#new').click(function () {
        window.top.xhTop.showModal({
            title:'新建合作模式',
            content:'<iframe   src="'+baseUrl+'customer/cooperationForm?id='+addressUri.getParam('id')+'"  style="width: 100%;height: 200px;border-style: none;padding: 0px;"  scrolling="yes"   ></iframe>',
            button:'提交',
            backClose:false});
    });
    
    //删除加载数据
    $('#tbody').on('click','[id^=delete_]',function () {
        // alert('delete');
        var iCUTId=$(this).attr("id").split("delete_")[1];

        myWindow.initPopUp({},function () {
            xhExtra.ajaxRequest('customer/deleteTraceData',{"iCUTId":iCUTId,'type':3,'iCUId':addressUri.getParam('id')},{},function (data) {
                initTable(data['data'],data['showDelete'],data['showEdit']);
            });
            // $.ajax({
            //     url : baseUrl+'customer/deleteTraceData',
            //     data : {"iCUTId":iCUTId,'type':3,'iCUId':addressUri.getParam('id')},
            //     dataType : 'JSON',
            //     type : 'POST',
            //     success:function (data) {
            //         initTable(data['data'],data['showDelete'],data['showEdit']);
            //     },
            //     error:function (result) {
            //         console.log(result);
            //     }
            // })
        });
    });

    
    
    function initTable(data,showDelete,showEdit) {
        total.empty();
        total.append('总共'+data.length+'记录');
        tbody.empty();
        for(var i=0;i<data.length;i++){
           var  content='<tr id="'+data[i].iCUTId+'"> ' +
                '<td>'+data[i].sYear+'</td>' +
                '<td>'+data[i].sGrade+'</td>' +
                '<td>'+data[i].sUsageMode+'</td>' +
                ' <td>'+data[i].sChargeMode+'</td>' +
                ' <td>'+data[i].sLevel+'</td>' +
                ' <td>'+data[i].sStatus+'</td> ' +
                '<td> '; 
                //'<a id="edit_'+data[i].iCUTId+'" class="btn my-btn btn-xs "  data-toggle="modal" data-target="#model"  >编辑</a>' +
                if(showEdit){
                    content+='<a id="edit_'+data[i].iCUTId+'" class="btn  modebtn btn-xs "  >编辑</a>' ;
                }
                if(showDelete){
                     content+=' <a id="delete_'+data[i].iCUTId+'" class="btn  modebtn btn-xs " >删除</a>' ;
                 }   
               content+= ' </td> </tr>';
            tbody.append(content);
        }
    }

    //编辑加载数据
    $('#table').on('click','[id^=edit_]',function () {
        var iCUTId=$(this).attr("id").split("edit_")[1];
        window.top.xhTop.showModal({
            title:'新建合作模式',
            content:'<iframe   src="'+baseUrl+'customer/cooperationForm?id='+addressUri.getParam('id')+'&tid='+iCUTId+'"  style="width: 100%;height: 200px;border-style: none;padding: 0px;"  scrolling="yes"   ></iframe>',
            button:'提交',
            backClose:false});
    });



});

